package com.moxiao.yfjh.module.blind.controller.app.controller.base;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.module.blind.controller.admin.basicSetting.vo.BlindBasicSettingRespVO;
import com.moxiao.yfjh.module.blind.controller.admin.commonphrases.vo.CommonPhrasesExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.level.vo.BlindMemberLevelExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.mechanism.vo.MemberMechanismExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.privilege.vo.MemberPrivilegeExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.recommendSetting.vo.RecommendSettingExportReqVO;
import com.moxiao.yfjh.module.blind.controller.app.controller.base.vo.AppMembershipSystemVo;
import com.moxiao.yfjh.module.blind.controller.app.controller.base.vo.AppRecommendReqVO;
import com.moxiao.yfjh.module.blind.convert.basicSetting.BlindBasicSettingConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.basicSetting.BlindBasicSettingDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.commonphrases.CommonPhrasesDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.level.BlindMemberLevelDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.mechanism.MemberMechanismDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.privilege.MemberPrivilegeDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.recommendSetting.RecommendSettingDO;
import com.moxiao.yfjh.module.blind.service.basicSetting.BlindBasicSettingService;
import com.moxiao.yfjh.module.blind.service.commonphrases.CommonPhrasesService;
import com.moxiao.yfjh.module.blind.service.level.BlindMemberLevelService;
import com.moxiao.yfjh.module.blind.service.mechanism.MemberMechanismService;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.blind.service.privilege.MemberPrivilegeService;
import com.moxiao.yfjh.module.blind.service.recommendSetting.RecommendSettingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.error;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/1/12 14:21
 **/

@Tag(name = "用户 APP - 用户基础设置相关数据")
@RestController
@RequestMapping("/blind/basic/setting")
@Validated
@Slf4j
public class AppBasicSettingController {

    @Resource
    private MemberMechanismService memberMechanismService;

    @Resource
    private MemberPrivilegeService memberPrivilegeService;

    @Resource
    private BlindMemberLevelService blindMemberLevelService;

    @Resource
    private CommonPhrasesService commonPhrasesService;

    @Resource
    private RecommendSettingService recommendSettingService;

    @Resource
    private BlindBasicSettingService basicSettingService;

    @Resource
    private BlindMemberUserService memberUserService;

    @GetMapping("/getMemberUserPrivilege")
    @Operation(summary = "获取会员尊享特权")
    public CommonResult<List<AppMembershipSystemVo>> getMemberUserPrivilege() {
        List<AppMembershipSystemVo> appMembershipSystemVos= new ArrayList<>();
        List<BlindMemberLevelDO> memberLevelList = blindMemberLevelService.getMemberLevelList(new BlindMemberLevelExportReqVO());
        if (CollectionUtils.isNotEmpty(memberLevelList)){

            memberLevelList.forEach(blindMemberLevelDO -> {
                AppMembershipSystemVo appMembershipSystemVo = new AppMembershipSystemVo();
                appMembershipSystemVo.setName(blindMemberLevelDO.getName());
                appMembershipSystemVo.setNo(blindMemberLevelDO.getNo());
                appMembershipSystemVo.setLevelId(blindMemberLevelDO.getId());

                MemberPrivilegeExportReqVO privilegeReqVO = new MemberPrivilegeExportReqVO();
                privilegeReqVO.setLevelId(blindMemberLevelDO.getId());
                //会员特权
                List<MemberPrivilegeDO> memberPrivilegeList = memberPrivilegeService.getMemberPrivilegeList(privilegeReqVO);
                List<Map<String, String>> memberPrivilegeMapList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(memberPrivilegeList)){
                    appMembershipSystemVo.setListPrivilege(memberPrivilegeList);
                    memberPrivilegeList.forEach(memberPrivilegeDO -> {
                        Map<String, String> memberPrivilegeMap = new HashMap<>();
                        //"text":"打招呼""10次/月"
                        memberPrivilegeMap.put("text", memberPrivilegeDO.getPrivilegeName());
                        if (Objects.equals(memberPrivilegeDO.getHavaUnlimited(), (byte) 1)) {
                            memberPrivilegeMap.put("value", memberPrivilegeDO.getPrivilegeNum());
                        } else {
                            memberPrivilegeMap.put("value", "-1");
                        }
                        memberPrivilegeMapList.add(memberPrivilegeMap);
                    });
                }
                //会员机制
                MemberMechanismExportReqVO mechanismReqVO = new MemberMechanismExportReqVO();
                mechanismReqVO.setLevelId(blindMemberLevelDO.getId());
                List<MemberMechanismDO> memberMechanismList = memberMechanismService.getMemberMechanismList(mechanismReqVO);
                List<Map<String, String>> memberMechanismMapList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(memberMechanismList)){
                    for (MemberMechanismDO memberMechanismDO : memberMechanismList) {
                        Map<String, String> memberMechanismMap = new HashMap<>();
                        memberMechanismMap.put("id", String.valueOf(memberMechanismDO.getId()));
                        String time = memberMechanismDO.getTime();
                        memberMechanismMap.put("text", time);
                        String money = String.valueOf(memberMechanismDO.getMoney());
                        memberMechanismMap.put("value", money);
                        //日均
                        long day = Long.parseLong(time) * 31;
                        double result = memberMechanismDO.getMoney().doubleValue() / day;
                        memberMechanismMap.put("num", String.valueOf(Math.round(result * 100.0) / 100.0));
                        memberMechanismMapList.add(memberMechanismMap);
                    }
                }
                if (CollectionUtils.isNotEmpty(memberPrivilegeMapList)) {
                    appMembershipSystemVo.setPrivilegeList(memberPrivilegeMapList);
                } else {
                    appMembershipSystemVo.setPrivilegeList(null);
                }
                appMembershipSystemVo.setMechanismList(memberMechanismMapList);
                appMembershipSystemVos.add(appMembershipSystemVo);
            });
        }
        return CommonResult.success(appMembershipSystemVos);
    }

    @GetMapping("/getCommonPhrasesList")
    @Operation(summary = "获取常用语列表")
    public CommonResult<List<String>> getCommonPhrasesList(){
        CommonPhrasesExportReqVO commonPhrasesExportReqVO = new CommonPhrasesExportReqVO();
        commonPhrasesExportReqVO.setStatus((byte) 1);
        List<CommonPhrasesDO> commonPhrasesList = commonPhrasesService.getCommonPhrasesList(commonPhrasesExportReqVO);
        return CommonResult.success(commonPhrasesList.stream().map(CommonPhrasesDO::getCommonPhrases).collect(Collectors.toList()));
    }

    @GetMapping("/getMemberRecommend")
    @Operation(summary = "获取超级推荐信息")
    public CommonResult<AppRecommendReqVO>  getMemberRecommendList(){
        BlindMemberUserDO memberUser = memberUserService.getMemberUser(getLoginUserId());
        if (Objects.isNull(memberUser)){
            return error(401, "获取个人信息失败");
        }
        AppRecommendReqVO appRecommendReqVO = new AppRecommendReqVO();
        appRecommendReqVO.setRecommendNum(memberUser.getRecommendNum());
        List<RecommendSettingDO> recommendSettingList = recommendSettingService.getRecommendSettingList(new RecommendSettingExportReqVO());
        List<Map<String,String>> recommendMechanismMapList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(recommendSettingList)){
            appRecommendReqVO.setBuyExplain(recommendSettingList.get(0).getBuyExplain());
            for (RecommendSettingDO recommendSettingDO : recommendSettingList) {
                Map<String, String> recommendMechanismMap = new HashMap<>();
                recommendMechanismMap.put("id",String.valueOf(recommendSettingDO.getId()));
                String num = recommendSettingDO.getNum();
                recommendMechanismMap.put("text", num);

                recommendMechanismMap.put("value", recommendSettingDO.getMoney().toString());
                double result = recommendSettingDO.getMoney().doubleValue() / Long.parseLong(num);
                recommendMechanismMap.put("num", String.valueOf(Math.round(result * 100.0) / 100.0));
                recommendMechanismMapList.add(recommendMechanismMap);
            }
            appRecommendReqVO.setRecommendMechanism(recommendMechanismMapList);
        }
        return CommonResult.success(appRecommendReqVO);
    }

    @GetMapping("/getBasicSetting")
    @Operation(summary = "获得相亲基础设置")
    public CommonResult<BlindBasicSettingRespVO> getBasicSetting() {
        BlindBasicSettingDO basicSetting = basicSettingService.getBasicSetting(1L);
        return success(BlindBasicSettingConvert.INSTANCE.convert(basicSetting));
    }


    public static void main(String[] args) {
        int day = 6*31;
        String money  = "100";
        DecimalFormat df = new DecimalFormat("#.00");
        System.out.println(Double.parseDouble(money) / day);
        System.out.println(df.format(Double.parseDouble(money) / day));
    }


}
